//
//  HHSuggestionViewController.m
//  HHBroker
//
//  Created by He Wei on 9/30/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHSuggestionViewController.h"
#import "SAMTextView.h"
#import "HttpRequestManager.h"

@interface HHSuggestionViewController () <UITextViewDelegate>

@property (nonatomic, strong) SAMTextView *commentTextView;

@end

@implementation HHSuggestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"提交"
                                                             style:UIBarButtonItemStylePlain
                                                            target:self
                                                            action:@selector(commentBtnClicked:)];
    self.navigationItem.rightBarButtonItem = item;
    [self.view addSubview:self.commentTextView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

- (void)commentBtnClicked:(UIButton *)sender
{
    __block MBProgressHUD *hub;
    NSString *commentString = self.commentTextView.text;
    if (!commentString.length) {
        hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hub.mode = MBProgressHUDModeText;
        hub.labelText = NSLocalizedString(@"请输入完整建议", @"");
        [hub hide:YES afterDelay:2];
        return;
    } else if (commentString.length > 200) {
        hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hub.mode = MBProgressHUDModeText;
        hub.labelText = NSLocalizedString(@"请保持200字以内", @"");
        [hub hide:YES afterDelay:2];
        return;
    }
    hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    commentString = [commentString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic =@{@"content":commentString};
    [HttpRequestManager postWithUrlString:kUrlFeedBack parameters:dic success:^(id response) {
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        hub.mode = MBProgressHUDModeText;

        if (code == 1) {
            hub.labelText = NSLocalizedString(@"反馈成功", @"");
                        hub.completionBlock = ^{
                [self.navigationController popViewControllerAnimated:YES];
            
            };
            [hub hide:YES afterDelay:1];

        } else {
            NSString *msg1 = [NSString stringWithFormat:@"%@", [response objectForKey:@"msg"]];
            hub.labelText = msg1;
            [hub hide:YES afterDelay:2];
        
        }
    
    }failure:^(NSError *error) {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"网络有点问题";
        [hub hide:YES afterDelay:2];
    
    } isLogin:NO];
    

}

#pragma mark - Setter && Getter
- (SAMTextView *)commentTextView
{
    if (_commentTextView == nil) {
        SAMTextView *comment = [[SAMTextView alloc] initWithFrame:CGRectMake(10, 64+10, self.view.frame.size.width-20, 117)];
        comment.font = [UIFont systemFontOfSize:14.0f];
        comment.delegate = self;
        comment.returnKeyType = UIReturnKeyDone;
        comment.placeholder = NSLocalizedString(@"请尽量详细描述", @"");
        comment.layer.borderColor = [UIColor colorWithRed:206/255.0 green:206/255.0 blue:206/255.0 alpha:1].CGColor;
        comment.layer.masksToBounds = YES;
        comment.tintColor = kJiaYuanColor;
        comment.layer.borderWidth = 0.5f;
        comment.layer.cornerRadius = 6.0f;
        comment.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        
        _commentTextView = comment;
        
    }
    return _commentTextView;
}


@end
